import axios from 'axios';
import { REQUEST, SUCCESS, FAILURE, ReducerActionType } from 'utils/action-type';
const namespace = 'ROOT_A';
export const ACTION_TYPES = {
    RESET: `${namespace}/RESET`,
    FETCH: `${namespace}/FETCH`
};

export const A_API_URL = {
    fetchData: 'getAData'
};
export interface STATE_A {
    loading?: boolean;
    data?: [];
}
const initialState: STATE_A = {
    loading: false,
    data: []
};

// Action
export const reset = (): any => ({
    type: ACTION_TYPES.RESET
});
export const fetchData = (id: string): any => ({
    type: ACTION_TYPES.FETCH,
    payload: axios.get(`${A_API_URL.fetchData}/xxx/${id}`)
});
// Reducer
const stateA = (state: STATE_A = initialState, action: ReducerActionType): STATE_A => {
    switch (action.type) {
    case ACTION_TYPES.RESET:
        return {
            ...state,
            loading: false,
            data: []
        };

    case REQUEST(ACTION_TYPES.FETCH): {
        const data = action.payload?.data;
        return {
            ...state,
            loading: true,
            data
        };
    }
    case SUCCESS(ACTION_TYPES.FETCH): {
        const data = action.payload?.data;
        return {
            ...state,
            loading: false,
            data
        };
    }
    case FAILURE(ACTION_TYPES.FETCH):
        return { ...state, loading: false };
    default:
        return state;
    }
};

export default stateA;
